<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace Topic\Controller;
use Common\Controller\AdminbaseController;
class AdminTopicController extends AdminbaseController {
	
	protected $topic_model;
	
	function _initialize() {
		parent::_initialize();
		$this->topic_model=D("Topic");
	}
	
	function index(){
		$topic_cat_model=M("TopicCat");
		
		$join = C('DB_PREFIX').'topic as b on a.id =b.cid';
		$join2 = C('DB_PREFIX').'users as c on c.id =b.uid';
		
		$where=array();
		
		$catid=I("get.cat",0,"intval");
		
		if(!empty($catid)){
			$where['a.id']=$catid;
		}
		
		$order=array("b.istop"=>"desc");
		
		$sort=I("get.sort",0,"intval");
		
		if(empty($sort)){
			$order['b.createtime']="desc";
		}else{
			$sort==1?$order['b.last_comment']="desc":"";//按最新回复
			if($sort==2){//精华
				$where['b.star']=array("gt",0);
				$order['b.createtime']="desc";
			}
		}
		
		$totalsize=$topic_cat_model->alias("a")->join($join)->where($where)->count();
		
		$page = $this->page($totalsize, 20);
		
		$topics=$topic_cat_model->field("a.cat_name,b.*,c.user_login,c.user_nicename")->alias("a")->join($join)->join($join2)
		->where($where)
		->order($order)
		->limit($page->firstRow . ',' . $page->listRows)
		->select();
		
		$this->assign("Page",$page->show('Admin'));
		
		$topic_cats=$topic_cat_model->where(array("status"=>1))->order("listorder asc")->select();
		
		$this->assign("topic_cats",$topic_cats);
		
		$catid=I("get.cat",0,"intval");
		if(!empty($catid)){
			$topic_cat=$topic_cat_model->where(array("id"=>$catid))->find();
			$this->assign("topic_cat",$topic_cat);
		}
		
		
		$this->assign("topics",$topics);
		
		$this->display();
		
	}
	
	public function edit(){
		$id=I("get.id");
		
		$topic_cat_model=M("TopicCat");
		
		$join = C('DB_PREFIX').'topic as b on a.id =b.cid';
		$join2 = C('DB_PREFIX').'users as c on c.id =b.uid';
		$topic=$topic_cat_model->field("a.cat_name,b.*,c.user_login,c.user_nicename")->alias("a")->join($join)->join($join2)
		->order("b.createtime desc")
		->where(array("b.id"=>$id))
		->find();
		
		$this->assign($topic);
		$this->display();
	}
	
	public function edit_post(){
		if (IS_POST) {
			if(!empty($_POST['photos_alt']) && !empty($_POST['photos_url'])){
				foreach ($_POST['photos_url'] as $key=>$url){
					$photourl=sp_asset_relative_url($url);
					$_POST['smeta']['photo'][]=array("url"=>$photourl,"alt"=>$_POST['photos_alt'][$key]);
				}
			}
			$_POST['smeta']['thumb'] = sp_asset_relative_url($_POST['smeta']['thumb']);
			$_POST['post']['smeta']=json_encode($_POST['smeta']);
			unset($_POST['post']['post_author']);
			$result=$this->topic_model->save($_POST['post']);
			//echo($this->topic_model->getLastSql());die;
			if ($result!==false) {
				$this->success("保存成功！");
			} else {
				$this->error("保存失败！");
			}
		}
	}
	
	function delete(){
		/* if(isset($_GET['tid'])){
			$tid = intval(I("get.tid"));
			$data['status']=0;
			if ($this->terms_relationship->where("tid=$tid")->save($data)) {
				$this->success("删除成功！");
			} else {
				$this->error("删除失败！");
			}
		}
		if(isset($_POST['ids'])){
			$tids=join(",",$_POST['ids']);
			$data['status']=0;
			if ($this->terms_relationship->where("tid in ($tids)")->save($data)) {
				$this->success("删除成功！");
			} else {
				$this->error("删除失败！");
			}
		} */
	}
	
	function check(){
		if(isset($_POST['ids']) && $_GET["check"]){
			$data["status"]=1;
				
			$ids=join(",",$_POST['ids']);
			if ( $this->topic_model->where("id in ($ids)")->save($data)!==false) {
				$this->success("审核成功！");
			} else {
				$this->error("审核失败！");
			}
		}
		if(isset($_POST['ids']) && $_GET["uncheck"]){
				
			$data["status"]=0;
			$ids=join(",",$_POST['ids']);
			if ( $this->topic_model->where("id in ($ids)")->save($data)) {
				$this->success("取消审核成功！");
			} else {
				$this->error("取消审核失败！");
			}
		}
	}
	
	function top(){
		if(isset($_POST['ids']) && $_GET["top"]){
			$data["istop"]=1;
	
			$ids=join(",",$_POST['ids']);
			if ( $this->topic_model->where("id in ($ids)")->save($data)!==false) {
				$this->success("置顶成功！");
			} else {
				$this->error("置顶失败！");
			}
		}
		if(isset($_POST['ids']) && $_GET["untop"]){
			$data["istop"]=0;
	
			$ids=join(",",$_POST['ids']);
			if ( $this->topic_model->where("id in ($ids)")->save($data)!==false) {
				$this->success("取消置顶成功！");
			} else {
				$this->error("取消置顶失败！");
			}
		}
	}
	
	function recommend(){
		if(isset($_POST['ids']) && $_GET["recommend"]){
			$data["recommended"]=1;
	
			$ids=join(",",$_POST['ids']);
			if ( $this->topic_model->where("id in ($ids)")->save($data)!==false) {
				$this->success("推荐成功！");
			} else {
				$this->error("推荐失败！");
			}
		}
		if(isset($_POST['ids']) && $_GET["unrecommend"]){
	
			$data["recommended"]=0;
			
			$ids=join(",",$_POST['ids']);
			if ( $this->topic_model->where("id in ($ids)")->save($data)!==false) {
				$this->success("取消推荐成功！");
			} else {
				$this->error("取消推荐失败！");
			}
		}
	}
	
	
	
}